package com.example.demo.service;

import com.example.demo.domain.dto.Classroom;
import com.example.demo.mapper.ManagerDao;
import com.example.demo.exception.PasswordErrorException;
import com.example.demo.utils.ParseUtil;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lizijian
 */
@Service
public class ManagerService {

  @Autowired
  private Gson gson;

  @Autowired
  private ManagerDao managerDao;

  public void putCourseClassRoom() throws PasswordErrorException, IOException {
    List<Classroom> allClassRoom = ParseUtil.getAllClassRoom();
    for (Classroom classRoom : allClassRoom) {
      managerDao.insertCourseClassRoom(1, classRoom.getClassroomName(), classRoom.getBuildingName());
    }
  }

  private int convertDayCharacterToNum(String dayString){
    int day=0;
    switch (dayString) {
      case "一":
        day = 1;
        break;
      case "二":
        day = 2;
        break;
      case "三":
        day = 3;
        break;
      case "四":
        day = 4;
        break;
      case "五":
        day = 5;
        break;
      case "六":
        day = 6;
        break;
      case "日":
        day = 7;
        break;
      default:
        System.out.println("星期处理错误");
    }
    return day;
  }

  public void putOptionalCourse() throws IOException {
    Response response = Jsoup.connect("https://dean.hbut.edu.cn/info/1006/2195.htm").execute();
    Document document = response.parse();
    Elements tr = document.select("tr");
    for (int j=0;j<tr.size();j++) {
      if(j==0){
        continue;
      }
      Element element = tr.get(j);
      Elements td = element.select("td");
      String courseName = td.get(1).text();
      String teacherName = td.get(3).text();
      String timeAndAddress = td.get(4).text();
      if (timeAndAddress.contains("请")) {
        continue;
      }
      String[] timeList = timeAndAddress.split(",");
      for (String time : timeList) {
        String[] s = time.split(" ");
        String address = s[0];
        String dayString = s[2];
        dayString = dayString.substring(2);
        String lessonString=s[3];
        lessonString=lessonString.substring(0,lessonString.length()-1);
        int lesson=0;
        if("NI".equals(lessonString)){
            lesson=5;
        }else if("34".equals(lessonString)){
            lesson=2;
        }else {
          System.out.println(lessonString);
          System.out.println("节次处理错误");
        }
        String weeksString=s[1];
        weeksString=weeksString.substring(0,weeksString.length()-1);
        Set<Integer> weeksSet=new HashSet<>(32);
        String[] split = weeksString.split(";");
        for (String finalWeek : split) {
          if(finalWeek.contains("-")){
            int startWeek=Integer.parseInt(finalWeek.substring(0,finalWeek.indexOf("-")));
            int endWeek= Integer.parseInt(finalWeek.substring(finalWeek.indexOf("-")+1));
            for (int i=startWeek;i<=endWeek;i++){
              weeksSet.add(i);
            }
          }else {
            weeksSet.add(Integer.parseInt(finalWeek));
          }
        }
        String weeks=gson.toJson(weeksSet);
        for (int k=0;k<dayString.length();k++){
          int day = convertDayCharacterToNum(String.valueOf(dayString.charAt(k)));
          managerDao.addOptionalCourse(courseName,teacherName,weeks,address,day,lesson);
        }
      }
    }
  }

  public static void main(String[] args) throws IOException {
    new ManagerService().putOptionalCourse();
  }

}
